//
//  BZUCUser.m
//  BZUCSDK
//
//  Created by 郭艺伟 on 2017/10/10.
//  Copyright © 2017年 郭艺伟. All rights reserved.
//

#import "BZUCUser.h"
#import "BZUCDataBaseManager.h"
@interface BZUCUser ()

/**
 *  只初始化一次
 */
@property (nonatomic, assign) BOOL onceToken;

@end

@implementation BZUCUser

+ (NSDictionary *)JSONKeyPathsByPropertyKey{
    
    return @{ @"customerId"      : @"customerId",
              @"username"        : @"username",
              @"nickname"        : @"nickname",
              @"realName"        : @"realName",
              @"phone"           : @"phone",
              @"portrait"        : @"portrait",
              @"type"            : @"type" ,
              @"sectName"        : @"sectName",
              @"sectId"          : @"sectId"  ,
              @"sessionId"       : @"sessionId",
              @"backgroundUrl"   : @"backgroundUrl",
              @"isHeadlineFirst" : @"isHeadlineFirst"
              };
}


+ (instancetype)shareInstance {
    
    static dispatch_once_t once;
    static id _singletonManager = nil;
    
    dispatch_once( &once, ^{
        _singletonManager = [[BZUCUser alloc] init];
    } );
    
    return _singletonManager;
}

- (instancetype)init {
    
    if (self = [super init]) {
        
        self.customerId = 0;
        
    }
    
    return self;
}

-(void)setupWithDict:(NSDictionary *)dict
{
    self.customerId = [[dict objectForKey:@"customerId"] longLongValue];
    self.username = [dict objectForKey:@"username"];
    self.nickname = [dict objectForKey:@"nickname"];
    self.realName = [dict objectForKey:@"realName"];
    self.phone = [dict objectForKey:@"phone"];
    self.portrait = [dict objectForKey:@"picture"];
    self.type = [[dict objectForKey:@"customerType"] integerValue];
    self.sectName = [dict objectForKey:@"sectName"];
    self.sectId = [[dict objectForKey:@"sectId"] integerValue];
    self.sessionId = [dict objectForKey:@"sessionId"];
    self.backgroundUrl = [dict objectForKey:@"backgroundUrl"];
    self.isHeadlineFirst = [[dict objectForKey:@"isHeadlineFirst"] boolValue];
    self.password = [dict objectForKey:@"password"];
    
}

- (void)clean
{
    self.customerId = 0;
    self.username = nil;
    self.nickname = nil;
    self.realName = nil;
    self.phone = nil;
    self.portrait = nil;
    self.type = 0;
    self.sectName = nil;
    self.status = 0;
    
    self.sectId = 0;
    self.password = nil;
    self.sessionId = nil;
    self.backgroundUrl = nil;
    self.expireTime = nil;
    self.isHeadlineFirst = NO;
}

-(BZUCUser *)localUser
{
    if (!_localUser) {
        _localUser = [BZUCDataBaseManager queryCurrentLoginUserWithError:nil];
        
        NSTimeInterval now = [[NSDate date] timeIntervalSince1970];
        if ([_localUser.expireTime longLongValue] < now) {
            _localUser.password = @"";
        }
    }
    
    return _localUser;

}



@end
